<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">

    <style>

        .nav {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }

        .container {
            padding-top: 80px;
            height: auto;
        }

        .container-right {
            width: auto;
        }

        .blog-pagnation-wrapper {
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }

        .blog-pagnation-item {
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }

        .blog-pagnation-item:hover {
            background: #4e4eeb;
            color: #fff;
        }

        .blog-pagnation-item.actvie {
            background: #4e4eeb;
            color: #fff;
        }

    </style>
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/jianyin.webp" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer"></span>
    <a href="blog_list.html">主页</a>
    <a href="myblog_list.html">我的文章</a>
    <a href="blog_edit.html">写博客</a>
    <a href="login.html">登陆</a>
    <!-- <a href="#">注销</a> -->
</div>
<!-- 版心 -->
<div class="container">
    <!-- 右侧内容详情 -->
    <div class="container-right">
        <!-- 每一篇博客包含标题, 摘要, 时间 -->
        <div id="artListDiv">

        </div>

        <hr>
        <div class="blog-pagnation-wrapper">
            <button onclick="goFirstPage()" class="blog-pagnation-item">首页</button>
            <button onclick="goBeforePage()" class="blog-pagnation-item">上一页</button>
            <button onclick="goNextPage()" class="blog-pagnation-item">下一页</button>
            <button onclick="goLastPage()" class="blog-pagnation-item">末页</button>
        </div>
    </div>
</div>
<script>
    var pindex = 1;
    var psize = 2;
    var maxpage = 1;
    pindex = (getUrlValue("pindex") == "" ? 1 : getUrlValue("pindex"));

    function initPage() {
        jQuery.ajax({
            url: "/art/listpage",
            type: "POST",
            data: {'pindex': pindex, "psize": psize},
            success: function (result) {
                if (result != null && result.code == 200) {
                    if (result.data != null && result.data.length > 0) {

                        var artListHTML = "";
                        for (var i = 0; i < result.data.length; i++) {
                            var artItem = result.data[i];
                            artListHTML += '<div class="blog">';
                            artListHTML += '<div class="title">' + artItem.title + '</div>';
                            artListHTML += '<div class="date">' + artItem.updatetime + '</div>';
                            artListHTML += '<div class="desc">';
                            artListHTML += artItem.content;
                            artListHTML += ' </div>';
                            artListHTML += '<a href="blog_content.html?id=' +
                                artItem.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';

                            artListHTML += '</div>';
                        }
                        jQuery("#artListDiv").html(artListHTML);
                    } else {
                        jQuery("#artListDiv").html("<h3>暂无文章~</h3>");
                    }
                } else {
                    alert("查询失败！");
                }
            }
        })
    }

    initPage();
    function getMaxPage() {
        jQuery.ajax({
            url:"art/artcount",
            type:"POST",
            data:"",
            success:function(result) {
                if(result != null && result.data > 0) {
                    
                    maxpage = Math.ceil(result.data/psize);                    
                }
            }
        })
    }
    getMaxPage();

    function goFirstPage() {
        location.href = "blog_list.html";
    }

    function goBeforePage() {
       
       
        if (pindex > 1) {
            pindex = parseInt(pindex) - 1;
            location.href = "blog_list.html?pindex=" + pindex; 
        } else {
            alert("当前已在首页！")
            location.href = "blog_list.html";
        }

    }

    function goNextPage() {
       
        pindex = parseInt(pindex) + 1;
        if(pindex <= maxpage) {
            location.href = "blog_list.html?pindex=" + pindex; 
        } else {
            location.href = "blog_list.html?pindex=" + maxpage;
            alert("当前已在最后一页！")
        }
        
        
    }
    function goLastPage() {
        location.href = "blog_list.html?pindex=" + maxpage;
    }
</script>
</body>
</html>